import java.io.IOException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
public class FeedControl {
	static final int THREE = 3;
	static int offset = 1000, vyvedeno = 0;
	//static final String[] keel={"et", "ru", "en", "be", "fr", "es", "eo", "vi"}; 
	static String lang;
	static boolean helpPar, nobufPar, revPar, offsetPar;
	//static boolean isOrigBuffered;
	static boolean buffered;
	static boolean conOk;
	static Buffer buffer;
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub;
		//lang= "vi";
		parseArgs (args);
		System.out.println("lang: " + lang);
		String open = "http://groups.google.com/group/iti0011/feed/rss_v2_0_msgs.xml";
		
		buffer = new Buffer ("test.xml");
		Connect con = new Connect(open);
		conOk = con.connection;
		FeedParserX feedParsed;
		if (!conOk) {
			if (buffer.fileIsNew) {
				System.out.println("No buffer available!");
				return;
			}
			feedParsed = new FeedParserX (buffer.toStr());
			boolean noMes = feedParsed.transform(lang);
			if (noMes) {
				System.out.println("No translates into this language in buffer available!");
				return;
			}
		} else {
			String html = con.getRawHtml();
			feedParsed = new FeedParserX(html);
		}		 
		revPar=true;
		feedParsed.sortDocByDates(revPar);
		String [] params = {lang, ""};
		resultsOut( feedParsed, params);
	}

	static void resultsOut (FeedParserX feedParsed, String [] params) {
		Document feedDoc = feedParsed.getDoc();
		NodeList pubDates = feedDoc.getElementsByTagName("pubDate");
		Dates nxDate = new Dates("");
		for (int i = 0; i  < pubDates.getLength(); i++){
			String [] curDateOut;
			nxDate.Str2Cal(pubDates.item(i).getTextContent() + "C");
			if (offsetPar && nxDate.DateDiffNow() > offset) continue;
			
			if (conOk) curDateOut = buffer.mkArrayToOut(pubDates.item(i), params[0]);
			else {curDateOut = feedParsed.toArr(pubDates.item(i)); }
			if (curDateOut[4] == "b") buffered = true;
			if (nobufPar && buffered) continue;
			if (buffered) System.out.println("[BUFFERED ENTRY]: ");
			for (int j = 0; j < curDateOut.length; j++) {
				//System.out.println(j + "--" + curDateOut[j]);
				if (j ==0) { System.out.println("*** " + curDateOut[j] + " ***"); }
				if (j ==1) { System.out.println(curDateOut[j]); }

				if (j == 2) {					
					System.out.println(TransformHTML.format(curDateOut[j], curDateOut[5]));
				}
			}
				System.out.println("**********");
				vyvedeno++;
				buffered = false;
			
		} 
		if (vyvedeno == 0) System.out.println ("No new messages!");
		else System.out.println("End of messages");
		buffer.toXml();
		
	}
	static void parseArgs (String [] args) {
		if 	(args.length == 0 || args [0].charAt(0) != '-' || 
				args[0].equals("-?") || args[0].equals("-help")) {
					showHelp();
		}
		for (int i = 0; i < args.length; i++) {
			if (args[i].equals("-nb")) { nobufPar = true;  }
			if (args[i].equals("-nb")) { revPar = true;  }
			if (args[i].equals("-l")) lang = args[i+1];
			if (args[i].equals("-o")) {
				offsetPar = true;
				offset = Integer.parseInt(args[i+1]);
			}
		}
	}
	static void showHelp () {
		System.out.println("OPTIONS:");
		System.out.println("-? or -help \tshows this message");
		System.out.println("-l <lg> \tsets language of  translation: en, ru, fr, es, be etc ");
		System.out.println("-nb \t\t\tshow only new messages");
		System.out.println("-o <offset> \tlimits history offset in days");
		System.out.println("-r  \t\t\thistory of messages (chronological order)");
		System.exit(0);
		
		
	}

}
